import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_high_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_picker_photo.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/hazard_management/hazard_confirmation/hazard_confirmation_controller.dart';
import 'package:url_launcher/url_launcher.dart';

/// 我的任务-隐患排查-隐患治理-隐患确认页面
class HazardConfirmationPage extends StatelessWidget {
  HazardConfirmationPage({super.key});

  final controller = Get.put(HazardConfirmationController());

  /// 获取展开按钮
  Widget _getExpandButton() {
    return Row(
      children: [
        Text(
          controller.isExpanded ? '收起' : '展开',
          style: TextStyle(
            fontSize: 14.sp,
            color: Colors.white,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap6,
        SvgPicture.asset(
          controller.isExpanded ? AppImages.icon_circle_up : AppImages.icon_circle_down,
          width: 16.w,
          height: 16.w,
        )
      ],
    ).clickOnce(controller.onExpanded);
  }

  /// 获取基础信息
  Widget _getBasicInformation() {
    return Container(
        width: AppDimens.sh,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
        color: const Color(0xFF5C6DFF),
        child: Column(
          children: [
            Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_title_task,
                  width: 17.w,
                  height: 17.w,
                ),
                AppGaps.hGap6,
                Expanded(
                    child: Text(
                  controller.infoEntity.troubleshootContent!,
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                    overflow: TextOverflow.ellipsis,
                    fontWeight: FontWeight.w500,
                  ),
                )),
              ],
            ),
            AppGaps.vGap12,
            Row(
              children: [
                Expanded(
                    child: Text(
                  '隐患描述：${controller.infoEntity.dangerRecordDesc}',
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Colors.white,
                    overflow: controller.isExpanded ? null : TextOverflow.ellipsis,
                    fontWeight: FontWeight.w400,
                  ),
                )),
                Container(
                  height: 14.w,
                  width: 2.w,
                  margin: EdgeInsets.only(right: 20.w, left: 10.w),
                  decoration: const BoxDecoration(
                      gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                    Color(0x00ffffff),
                    Color(0xffffffff),
                    Color(0x00ffffff),
                  ])),
                ),
                _getExpandButton()
              ],
            )
          ],
        ));
  }

  /// 获取详情视图
  Widget _getDetailsView() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '拍照:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          Wrap(
              spacing: 5.w,
              children: controller.infoEntity.dangerImages!.map<Widget>((item) {
                return Container(
                  width: 74.w,
                  height: 74.w,
                  margin: EdgeInsets.only(bottom: 10.w),
                  decoration: BoxDecoration(
                    color: const Color(0xffF7F7FB),
                    border: Border.all(
                      color: const Color(0xffEBEDF0),
                    ),
                    borderRadius: BorderRadius.circular(8.w),
                    image: DecorationImage(
                      image: NetworkImage(Interface.fileUrl + item),
                      fit: BoxFit.cover,
                    ),
                  ),
                ).clickShowLarge(url: Interface.fileUrl + item);
              }).toList()),
          AppGaps.vGap12,
          Text(
            '视频:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          Wrap(
              spacing: 5.w,
              children: controller.infoEntity.dangerVideo!.map<Widget>((item) {
                return Container(
                  width: 160.w,
                  height: 90.w,
                  margin: const EdgeInsets.only(bottom: 10),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: const Color(0xff060B15),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: IconButton(
                    icon: const Icon(
                      Icons.play_circle,
                      color: Colors.white,
                      size: 30,
                    ),
                    onPressed: () {
                      FileUtils.openFile(Interface.fileUrl + item);
                    },
                  ),
                );
              }).toList()),
          AppGaps.vGap12,
          Text(
            '语音:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          controller.infoEntity.dangerVoice!.isNotEmpty
              ? CustomAudioplayers(
                  audioUrl: controller.infoEntity.dangerVoice!,
                  isEnabled: false,
                )
              : Container(),
          Visibility(
              visible: controller.isExpanded,
              child: Column(
                children: [
                  AppGaps.vGap14,
                  Row(
                    children: [
                      _getFieldView('隐患上报人：', controller.infoEntity.registrant!),
                      _getFieldView('电话：', controller.infoEntity.registrantUserPhone!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析对象：', controller.infoEntity.riskObjectName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('责任部门：', controller.infoEntity.hazardDep!),
                      _getFieldView('责任人：', controller.infoEntity.hazardLiablePerson!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析单元：', controller.infoEntity.riskUnitName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险事件：', controller.infoEntity.riskEventName!),
                      _getCurrentRiskLevel(),
                      // _getFieldView('当前风险等级：', '低风险', rightColor: const Color(0xFF5C6DFF)),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险描述：', controller.infoEntity.riskDescription!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('初始风险后果：', '${controller.infoEntity.initialRiskConsequences}'),
                      _getFieldView('初始风险度：', '${controller.infoEntity.initialRiskDegree}'),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getInitialRiskLevel(),
                      _getFieldView('管控措施：', '${controller.infoEntity.riskMeasureDesc}'),
                    ],
                  ),
                ],
              )),
        ],
      ),
    );
  }

  /// 当前风险等级
  Widget _getCurrentRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.currentRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('当前风险等级：', name, rightColor: rightColor);
  }

  /// 初始风险等级
  Widget _getInitialRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.initialRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('初始风险等级：', name, rightColor: rightColor);
  }

  /// 字段显示
  Widget _getFieldView(String title, String value, {Color? rightColor}) {
    return Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: const Color(0xFFA5A6B5),
              fontWeight: FontWeight.w400,
            ),
          ),
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: rightColor ?? const Color(0xFF495061),
              fontWeight: FontWeight.w400,
            ),
          ))
        ],
      ),
    );
  }
  /// 是否存在隐患
  Widget _getStateView() {
    return Obx(() {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 45.w),
        height: 64.w,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
              left: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 0 ? AppImages.icon_normal : AppImages.icon_un_normal,
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '正常',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 0 ? const Color(0xFFffffff) : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(0);
              }),
            ),
            Positioned(
              right: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 1 ? AppImages.icon_existence : AppImages.icon_un_existence,
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '存在隐患',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 1 ? const Color(0xFFffffff) : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(1);
              }),
            ),
          ],
        ),
      );
    });
  }

  /// 获取隐患确认显示
  Widget _getHazardConfirmationView() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          AppGaps.vGap10,
          // 是否存在隐患按钮
          _getStateView(),
          Divider(
            height: 1.w,
            color: const Color(0xFFECEEF3),
          ), //分割线
          Obx(() {
            return controller.currentIndex.value == 0
                ? _getNormalView() // 正常
                : _getExistenceView(); // 存在隐患
          }),
        ],
      ),
    );
  }

  /// 正常状态数据填写
  Widget _getNormalView() {
    return Column(
      children: [
        CustomHighInputPage(
          title: '驳回意见：',
          text: controller.infoEntity.checkAcceptComment,
          hintText: '请输入意见',
          onCallBack: (value) {
            controller.infoEntity.checkAcceptComment = value;
          },
        ),
        AppGaps.vGap6,
        CustomPickerPhoto(
          title: '拍照:',
          imagesList: controller.infoEntity.checkAcceptImages!,
          titleTextStyle: TextStyle(
            color: const Color(0xFF262F38),
            fontSize: 14.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
      ],
    );
  }

  /// 存在隐患
  Widget _getExistenceView() {
    return Column(
      children: [
        CustomInputPage(
          title: '隐患名称：',
          hintText: '请输入隐患名称',
          text: controller.infoEntity.dangerName!,
          onCallBack: (value) {
            controller.infoEntity.dangerName = value;
          },
        ),
        CustomSelectPage(
          selectText: controller.infoEntity.dangerLevel!,
          title: '隐患等级：',
          onCallBack: controller.selectDangerLevel,
        ),
        CustomSelectPage(
          selectText: controller.infoEntity.hazardDangerType!,
          title: '隐患类型：',
          onCallBack: controller.selectDangerType,
        ),
        CustomSelectPage(
          selectText: controller.infoEntity.dangerSrc!,
          title: '隐患来源：',
          onCallBack: controller.selectDangerSource,
        ),
        CustomSelectPage(
          selectText: controller.infoEntity.dangerManageType!,
          title: '治理类型：',
          onCallBack: controller.selectDangerManageType,
        ),
        CustomHighInputPage(
            title: '隐患描述：',
            text: controller.infoEntity.dangerDesc!,
            hintText: '请输入描述',
            onCallBack: (value) {
              controller.infoEntity.dangerDesc = value;
            }),
        CustomHighInputPage(
            title: '原因分析：',
            isRequired: false,
            text: controller.infoEntity.dangerReason!,
            hintText: '请输入原因分析',
            onCallBack: (value) {
              controller.infoEntity.dangerReason = value;
            }),
        CustomHighInputPage(
          title: '控制措施：',
          isRequired: false,
          text: controller.infoEntity.controlMeasures!,
          hintText: '请输入控制措施',
          onCallBack: (value) {
            controller.infoEntity.controlMeasures = value;
          },
        ),
        CustomInputPage(
          title: '资金（万元）：',
          hintText: '请输入资金',
          isRequired: false,
          text: controller.infoEntity.cost!,
          textInputType: TextInputType.number,
          onCallBack: (value) {
            controller.infoEntity.cost = value;
          },
        ),
        CustomSelectPage(
            selectText: controller.infoEntity.dangerManageDeadline!,
            title: '隐患整改期限：',
            onCallBack: () {
              controller.showDatePicker();
            }),
        CustomSelectPage(
            selectText: controller.infoEntity.liablePerson!,
            title: '整改负责人：',
            onCallBack: () {
              controller.selectResponsible();
            }),
        CustomSelectPage(
            selectText: controller.infoEntity.checkAcceptPerson!,
            title: '验收人：',
            onCallBack: () {
              controller.selectAcceptance();
            }),
        AppGaps.vGap10,
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '隐患确认',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                ),
              ),
              leading: const BackButton(
                color: Colors.white,
              ),
              iconButtonColor: Colors.white,
              backgroundColor: const Color(0xFF5C6DFF),
            ),
            body: Container(
                color: const Color(0xFFF5F5F6),
                child: Stack(
                  children: [
                    Container(
                      height: 144.w,
                      width: AppDimens.sh,
                      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
                      color: const Color(0xFF5C6DFF),
                    ),
                    Container(
                      alignment: Alignment.topCenter,
                      child: Column(
                        children: [
                          // 基础信息
                          _getBasicInformation(),
                          Expanded(
                            child: SingleChildScrollView(
                              child: Container(
                                padding: EdgeInsets.symmetric(horizontal: 16.w),
                                child: Column(
                                  children: [
                                    // 隐患数据
                                    _getDetailsView(),
                                    AppGaps.vGap14,
                                    // 隐患确认
                                    _getHazardConfirmationView(),
                                    SizedBox(height: 80.w)
                                  ],
                                ),
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                )),
            bottomSheet: Container(
              height: 60.w,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
                    offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
                    blurRadius: 10.0, // 阴影的模糊半径
                    spreadRadius: 2.0, // 阴影的扩散半径
                  ),
                ],
              ),
              child: Container(
                  height: 40.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: const Color(0xff5C6DFF),
                      borderRadius: BorderRadius.all(
                        Radius.circular(20.w),
                      )),
                  child: Text(
                    '确定',
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: const Color(0xffffffff),
                    ),
                  )).clickOnce(controller.complete),
            ),
          );
        });
  }
}
